package com.yoomiito.app.widget.dialog;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding3.view.RxView;
import com.yoomiito.app.R;
import com.yoomiito.app.model.bean.AppInterfaceInfo;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.wx.WXUtils;
import com.yoomiito.app.widget.SelectView;

import java.util.concurrent.TimeUnit;

public class PayDialog extends BaseDialog implements View.OnClickListener {
    private final int MONEY_PAY = 1;
    private final int WX_PAY = 2;
    private final int ALI_PAY = 3;
    private final int MONEY_WX_PAY = 4;
    private final int MONEY_ALI_PAY = 5;
    private final int OFFLINE = 6;
    private final int QUICK = 7;

    private OnResultListener mOnResultListener;

    private TextView mPayMoneyTv;
    private TextView mBalanceTv;
    private SelectView mPayMoneySv;
    private SelectView mPayQuickSv;
    private ImageView mIconPayMoneyIv;
    private TextView mIconPayMoneyTitleTv;
    private SelectView mPayWXSv;
    private SelectView mPayAliSv;
    private SelectView mPayOffline;

    private String balance = "";//余额
    private String payMoney = "";//待支付
    private boolean radio;//
    private boolean radio1;//

    private boolean isMoneyPay = true;
    private LinearLayout mPayMoneyLl,moneyWay;
    private boolean showPayMoney = true;
    private boolean installWX;


    private TextView tvBalance;

    private TextView tvName, tvPhone;


    AppInterfaceInfo appInterfaceInfo;
    private LinearLayout llWx, llZfb, llOffline,llQuick;


    public PayDialog(@NonNull Context context) {
        this(context, 0);
    }

    public PayDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.dialog_2);
    }

    @Override
    public View setView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_pay, null, false);
        ImageView closeIv = view.findViewById(R.id.close);
//        closeIv.setOnClickListener(this);
        RxView.clicks(closeIv)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(unit -> {
                    dismiss();
                    setResult(true);
                });
        view.findViewById(R.id.pay).setOnClickListener(this);
        installWX = WXUtils.INSTANCE.isInstallWX((Activity) mContext);
        if (!installWX) {
            view.findViewById(R.id.wx_pay_ll).setVisibility(View.GONE);
        }
        llZfb = view.findViewById(R.id.aLiPayLl);
        llOffline = view.findViewById(R.id.ll_offline);
        llQuick =  view.findViewById(R.id.quickpay);
        llWx = view.findViewById(R.id.wx_pay_ll);
        mPayMoneyTv = view.findViewById(R.id.pay_money);
        mBalanceTv = view.findViewById(R.id.useing_remain);
        mBalanceTv.setText("(剩余¥" + balance + ")");

        mPayMoneyLl = view.findViewById(R.id.pay_money_ll);
        mPayMoneyLl.setVisibility(showPayMoney ? View.VISIBLE : View.GONE);
        mIconPayMoneyIv = view.findViewById(R.id.icon_money_pay);
        mIconPayMoneyTitleTv = view.findViewById(R.id.icon_money_pay_title);
        mPayMoneySv = view.findViewById(R.id.item_pay_money);
        mPayQuickSv = view.findViewById(R.id.item_pay_quick);
        moneyWay = view.findViewById(R.id.ll_money_way);
        tvBalance = view.findViewById(R.id.tv_balance);
        tvBalance.setText(balance+"元");
        setUnClickMoneyPay();
        mPayMoneyTv.setText(payMoney);
        mPayWXSv = view.findViewById(R.id.item_pay_wx);
        mPayAliSv = view.findViewById(R.id.item_pay_ali);
        mPayOffline = view.findViewById(R.id.item_offline);

        tvName = view.findViewById(R.id.tv_name);
        tvPhone = view.findViewById(R.id.tv_phone);
        LinearLayout llWay = view.findViewById(R.id.ll_offline_way);

        mPayMoneySv.setChangeListener((view1, isSelected) -> {
            mPayOffline.setSelect(false);
            llWay.setVisibility(View.GONE);
            moneyWay.setVisibility(View.VISIBLE);
            changeWay(MONEY_PAY, isSelected);
        });
        mPayWXSv.setChangeListener((view1, isSelected) -> {
            llWay.setVisibility(View.GONE);
            moneyWay.setVisibility(View.GONE);
            changeWay(WX_PAY, isSelected);
        });
        mPayAliSv.setChangeListener((view1, isSelected) -> {
            llWay.setVisibility(View.GONE);
            moneyWay.setVisibility(View.GONE);
            changeWay(ALI_PAY, isSelected);
        });
        mPayOffline.setChangeListener((view1, isSelected) -> {
            llWay.setVisibility(View.VISIBLE);
            moneyWay.setVisibility(View.GONE);
            changeWay(OFFLINE, isSelected);
        });
        mPayQuickSv.setChangeListener((view1, isSelected) -> {
            llWay.setVisibility(View.GONE);
            moneyWay.setVisibility(View.GONE);
            changeWay(QUICK, isSelected);
        });
        setWidthPercent(1);
        setmHeightPercent(0.65);
        setmGravity(Gravity.BOTTOM);
        setDefaultSelect();
        showLin();
        setDefaultSelect();
        return view;
    }

    @Override
    public void onBackPressed() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pay:
                if (AllUtils.isFastClick()) {
                    return;
                }
                setResult(false);
                break;
        }

    }

    private void setResult(boolean dismiss) {
        if (mOnResultListener != null) {
            mOnResultListener.onResult(mPayMoneySv.isSelected() ? 1 : 0,
                    mPayWXSv.isSelected() ? 1 : 0,
                    mPayAliSv.isSelected() ? 1 : 0,
                    mPayOffline.isSelected() ? 1 : 0,
                    mPayQuickSv.isSelected() ? 1 : 0,
                    dismiss);
        }
    }

    public void setRadio(boolean radio) {
        this.radio = radio;
        setDefaultSelect();
    }


    private void changeWay(int payType, boolean selected) {
        if (payType == OFFLINE) {
            mPayWXSv.setSelect(false);
            mPayAliSv.setSelect(false);
            mPayMoneySv.setSelect(false);
            mPayQuickSv.setSelect(false);
        }  else if (payType == QUICK){
            mPayWXSv.setSelect(false);
            mPayAliSv.setSelect(false);
            mPayMoneySv.setSelect(false);
            mPayOffline.setSelect(false);
        } else {
            if (radio || appInterfaceInfo.blendPay == 0) {//单选
                if (isMoneyPay) mPayMoneySv.setSelect(payType == MONEY_PAY ? true : false);
                mPayWXSv.setSelect(payType == WX_PAY ? true : false);
                mPayAliSv.setSelect(payType == ALI_PAY ? true : false);
                mPayOffline.setSelect(false);
                mPayQuickSv.setSelect(false);
            } else {//混选
                if (payType == MONEY_PAY) {
                    return;
                }
                mPayWXSv.setSelect(payType == WX_PAY ? true : false);
                mPayAliSv.setSelect(payType == ALI_PAY ? true : false);
                mPayOffline.setSelect(false);
                mPayQuickSv.setSelect(false);
            }
        }

    }

    public void setOnResultListener(OnResultListener onResultListener) {
        mOnResultListener = onResultListener;
    }

    public interface OnResultListener {
        void onResult(int moneyPay, int wxPay, int aliPay, int offline,int quick, boolean dismiss);
    }

    public void setBalance(String balance) {
        this.balance = balance;
        if (mBalanceTv != null) mBalanceTv.setText("(剩余¥" + balance + ")");
        if(tvBalance != null) tvBalance.setText(balance+"元");
        setUnClickMoneyPay();
    }


    public void setShow(AppInterfaceInfo appInterfaceInfo) {
        this.appInterfaceInfo = appInterfaceInfo;
        if (llZfb == null || llWx == null || llOffline == null || llQuick == null)
            return;
        showLin();

    }


    private void showLin() {
        if (appInterfaceInfo == null)
            return;
        if (appInterfaceInfo.online == 1) {
            llZfb.setVisibility(View.VISIBLE);
            llWx.setVisibility(View.VISIBLE);
            if (appInterfaceInfo.wxPay == 1) {
                if (!installWX) {
                    llWx.setVisibility(View.GONE);
                } else {
                    llWx.setVisibility(View.VISIBLE);
                }
            } else {
                llWx.setVisibility(View.GONE);
            }
            if (appInterfaceInfo.zfbPay == 1) {
                llZfb.setVisibility(View.VISIBLE);
            } else {
                llZfb.setVisibility(View.GONE);
            }
        } else {
            llZfb.setVisibility(View.GONE);
            llWx.setVisibility(View.GONE);
        }
        if (appInterfaceInfo.offline == 1) {
            llOffline.setVisibility(View.VISIBLE);
        } else {
            llOffline.setVisibility(View.GONE);
        }
        if (appInterfaceInfo.fastPay == 1) {
            llQuick.setVisibility(View.VISIBLE);
        } else {
            llQuick.setVisibility(View.GONE);
        }
        if (tvName != null)
            tvName.setText(appInterfaceInfo.nickname);
        if (tvPhone != null)
            tvPhone.setText(appInterfaceInfo.mobile);
    }

    public void setPayMoney(String payMoney) {
        this.payMoney = payMoney;
        if (mPayMoneyTv != null) mPayMoneyTv.setText(payMoney);
    }

    private void setDefaultSelect() {
        if (mPayMoneySv == null || mPayWXSv == null || mPayAliSv == null || mPayOffline == null) {
            return;
        }
        if (appInterfaceInfo.wxPay == 1 && installWX) {
            mPayWXSv.setSelect(true);
            mPayMoneySv.setSelect(false);
            mPayAliSv.setSelect(false);
            mPayOffline.setSelect(false);
            mPayQuickSv.setSelect(false);
            return;
        }
        if (appInterfaceInfo.zfbPay == 1) {
            mPayWXSv.setSelect(false);
            mPayMoneySv.setSelect(false);
            mPayAliSv.setSelect(true);
            mPayOffline.setSelect(false);
            mPayQuickSv.setSelect(false);
            return;
        }
        if (radio && mPayMoneySv != null) {//余额足够
            mPayMoneySv.setSelect(true);
            mPayWXSv.setSelect(false);
            mPayAliSv.setSelect(false);
            mPayQuickSv.setSelect(false);
        } else if (!radio && mPayMoneySv != null && mPayWXSv != null) {
            if (isMoneyPay) mPayMoneySv.setSelect(true);
            if (installWX) {
                mPayWXSv.setSelect(true);
                mPayAliSv.setSelect(false);
            } else {
                mPayAliSv.setSelect(true);
            }
        }
    }

    private void setUnClickMoneyPay() {
        if (mPayMoneySv != null) {
            if (SpannableUtils.isEmptyOr0(balance)) {
                isMoneyPay = false;
                mPayMoneySv.setUnClickSelect(R.drawable.icon_unselect_gray);
                mIconPayMoneyTitleTv.setEnabled(false);
                mIconPayMoneyIv.setEnabled(false);
            } else {
                isMoneyPay = true;
                mPayMoneySv.setEnabled(true);
                mIconPayMoneyTitleTv.setEnabled(true);
                mIconPayMoneyIv.setEnabled(true);
            }
        }
    }

    public void hideMoneyPay() {
        showPayMoney = false;
    }
}
